import {
  _path,
  Write_player,
  existplayer,
  bound,
  Write_cangku,
  _item
} from '../../mode/allapi'
import { plugin, AMessage } from '../../api/api'
export class decompose extends plugin {
  constructor() {
    super({
      dsc: '分解',
      rule: [
        {
          reg: /^(#|\/)?分解.*$/,
          fnc: 'fenjie'
        }
      ]
    })
  }

  async fenjie(e: AMessage) {
    const id = e.user_id
    if (!(await existplayer(id))) return false
    const expecItem = e.msg.replace(/(#|\/)?分解/, '')
    const cangku = await (await _path(id)).cangku
    const player = await (await _path(id)).player
    const foundHun = await bound('将魂', cangku, 'name')
    const bingyin = await (await _path(id)).bingyin
    const foundJiang = await bound(expecItem, bingyin, 'name')
    if (!foundJiang) return e.reply(`您没有【${expecItem}】，或该角色已上阵`)
    player.militaryNum -= 1
    foundJiang.num -= 1
    const good = (await _item(id, 'goods')).find(item => item.id == 50002)
    if (foundHun) {
      foundHun.num += 40
    } else {
      good.num = 40
      cangku.push(good)
    }
    await Write_player(id, player)
    await Write_cangku('cangku', id, cangku)
    await Write_cangku('barracks', id, bingyin)
    e.reply(`已成功将【${expecItem}】转化为将魂*40`)
    return true
  }
}
